package ui.windows;

import entities.Department;
import entities.User;
import javax.swing.JTable;
import manager.AdminBusiDriver;
import model.ChoseDoctorTableModel;
import model.ViewDepartmentComboBoxModel;
import ui.panels.CreatePMaterialPanel;

/**
 * 
 * @author viev
 * This class used to show a dialog used to shoose a Doctor.
 */
public class ChoseDoctorDialog extends javax.swing.JDialog {

    /** Creates new form ChoseDoctorDialog */
    public ChoseDoctorDialog(RemedySchedulerFrame mainFrame, CreatePMaterialPanel parentPanel) {
        initComponents();
        this.mainFrame = mainFrame;
        this.parentPanel = parentPanel;
        includeModel();
    }

    /**
     * Include model.
     */
    private void includeModel() {
        depModel = new ViewDepartmentComboBoxModel(mainFrame.getGenericDriver().showDepartment());
        depModel.removeElementAt(0);
        depModel.removeElement(0);
        Department vit = new Department();
        vit.setDepartmentID(0);
        vit.setName("All");
        depModel.addElementAt(0, vit);
        cbDep.setModel(depModel);
        cbDep.setSelectedIndex(0);

        driver = (AdminBusiDriver) mainFrame.getDriver();
        doctorModel = new ChoseDoctorTableModel(driver.showDoctor());
        doctorTable.setModel(doctorModel);
        doctorTable.setFillsViewportHeight(true);
        doctorTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        doctorTable.getColumnModel().getColumn(0).setPreferredWidth(70);
        doctorTable.getColumnModel().getColumn(1).setPreferredWidth(180);
        doctorTable.getColumnModel().getColumn(2).setPreferredWidth(100);
        doctorTable.getColumnModel().getColumn(3).setPreferredWidth(210);
    }

    /**
     * filter doctors by department.
     */
    private void filter() {
        try {
            Department dep = (Department) cbDep.getSelectedItem();
            doctorModel.filter(dep.getDepartmentID());
        } catch (Exception e) {
        }
    }

    /**
     * Set selected record of doctor's information table.
     */
    private void setSelectedDoctor() {
        int rows[] = doctorTable.getSelectedRows();
        if (rows != null && rows.length == 1) {
            selectedDoctor = doctorModel.getAt(rows[0]);
            bChose.setEnabled(true);
        } else {
            selectedDoctor = null;
            bChose.setEnabled(false);
        }
    }

    /**
     * Method used to choose a doctor.
     */
    private void chose() {
        parentPanel.setSelectedDoctor(selectedDoctor);
        this.dispose();
    }

    /**
     * close dialog.
     */
    private void cancel() {
        this.dispose();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbDep = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        bCancel = new javax.swing.JButton();
        bChose = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        doctorTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Chose Employee");

        jLabel1.setText("Department:");

        cbDep.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbDep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbDepActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbDep, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(339, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbDep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        bCancel.setText("Cancel");
        bCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelActionPerformed(evt);
            }
        });

        bChose.setText("Chose");
        bChose.setEnabled(false);
        bChose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bChoseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(387, Short.MAX_VALUE)
                .addComponent(bChose, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bCancel)
                    .addComponent(bChose))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        doctorTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        doctorTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                doctorTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(doctorTable);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cbDepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDepActionPerformed
    filter();
}//GEN-LAST:event_cbDepActionPerformed

private void bChoseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bChoseActionPerformed
    chose();
}//GEN-LAST:event_bChoseActionPerformed

private void bCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelActionPerformed
    cancel();
}//GEN-LAST:event_bCancelActionPerformed

private void doctorTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_doctorTableMouseClicked
    setSelectedDoctor();
}//GEN-LAST:event_doctorTableMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChoseDoctorDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChoseDoctorDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChoseDoctorDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChoseDoctorDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                //
            }
        });
    }
    private RemedySchedulerFrame mainFrame;
    private CreatePMaterialPanel parentPanel;
    private AdminBusiDriver driver;
    private ViewDepartmentComboBoxModel depModel;
    private ChoseDoctorTableModel doctorModel;
    private User selectedDoctor;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCancel;
    private javax.swing.JButton bChose;
    private javax.swing.JComboBox cbDep;
    private javax.swing.JTable doctorTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
